package com.kool.kadmin.system.service;

import com.kool.kadmin.common.bean.ListResult;
import com.kool.kadmin.common.bean.Result;
import com.kool.kadmin.common.constant.ErrorCode;
import com.kool.kadmin.common.exception.AppException;
import com.kool.kadmin.common.util.ResultUtil;
import com.kool.kadmin.system.bean.SysMenuDo;
import com.kool.kadmin.system.constant.MenuStateEnum;
import com.kool.kadmin.system.dao.SysMenuAutDao;
import com.kool.kadmin.system.dao.SysMenuDao;
import com.kool.kadmin.system.request.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author luyu
 */
@Service
@Slf4j
public class MenuService {
    @Resource
    private SysMenuDao menuDao;
    @Resource
    private SysMenuAutDao menuAutDao;

    /**
     * 查询所有菜单
     *
     * @return
     */
    public ListResult<SysMenuDo> getAllMenus() {
        log.debug("查询所有菜单");
        List<SysMenuDo> menuDos = menuDao.getAll();

        return ResultUtil.success(menuDos);
    }

    /**
     * 增加菜单
     *
     * @param request
     * @return
     */
    public Result addMenu(AddMenuReq request) {
        SysMenuDo menuDo = new SysMenuDo();
        menuDo.setMenuCode(request.getMenuCode())
                .setTitle(request.getTitle())
                .setFatherCode(request.getFatherCode())
                .setRootCode(request.getRootCode())
                .setDeep(request.getDeep())
                .setSeq(request.getSeq())
                .setUrl(request.getUrl())
                .setViewPath(request.getViewPath())
                .setIcon(request.getIcon())
                .setState(MenuStateEnum.ACTIVE.getCode());
        menuDao.insert(menuDo);
        return ResultUtil.success();
    }

    /**
     * 修改菜单
     *
     * @param request
     * @return
     */
    public Result modifyMenu(ModifyMenuReq request) {
        SysMenuDo menuDo = new SysMenuDo();
        menuDo.setId(request.getId())
                .setTitle(request.getTitle())
                .setSeq(request.getSeq())
                .setUrl(request.getUrl())
                .setViewPath(request.getViewPath())
                .setIcon(request.getIcon());
        menuDao.updateInfo(menuDo);
        return ResultUtil.success();
    }

    /**
     * 删除菜单
     *
     * @param request
     * @return
     */
    public Result deleteMenu(DeleteMenuReq request) {

        String menuCode = request.getMenuCode();

        //查询是否有子菜单，如果有子菜单，不允许删除
        List<SysMenuDo> subMenus = menuDao.getSubMenus(menuCode);
        if (subMenus != null && subMenus.size() > 0) {
            throw new AppException(ErrorCode.BUSINESS_ERROR, "含有子菜单的菜单不能删除，请先删除子菜单");
        }

        //删除
        menuDao.deleteByCode(menuCode);
        return ResultUtil.success();
    }

    /**
     * 查询用户有权限的菜单
     *
     * @param request
     * @return
     */
    public ListResult<SysMenuDo> getUserMenus(GetUserMenusReq request) {
        List<SysMenuDo> menuDos = menuDao.getUserMenus(request.getUserId());
        return ResultUtil.success(menuDos);
    }

    /**
     * 查询权限绑定的菜单
     *
     * @param request
     * @return
     */
    public ListResult<SysMenuDo> getAutMenus(GetAutMenusReq request) {
        String autCode = request.getAutCode();
        List<SysMenuDo> menuDos = menuAutDao.getMenuByAutCode(autCode);
        return ResultUtil.success(menuDos);
    }
}
